package com.ruoyi.base.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.base.domain.Industry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.base.domain.AccountSet;
import com.ruoyi.base.service.IAccountSetService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 账套列表Controller
 * 
 * @author ruoyi
 * @date 2025-02-23
 */
@RestController
@RequestMapping("/accountSet")
public class AccountSetController extends BaseController
{
    @Autowired
    private IAccountSetService accountSetService;


    /**
     * api接口  通过用户id查询当前使用的账套id
     * @param userId
     * @return
     */
    @GetMapping("/getDefaultAccountByUserId/{userId}")
    public AjaxResult getDefaultAccountByUserId(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(accountSetService.getDefaultAccountByUserId(userId));
    }


    /**
     * 修改默认账套状态
     * @param accountId
     * @return
     */
    @PostMapping("/chaneDefaultStatus/{accountId}")
    public AjaxResult chaneDefaultStatus(@PathVariable Long accountId)
    {
        if (accountId == null) {
            throw new RuntimeException("账套id为空，不能修改");
        }
        return toAjax(accountSetService.chaneDefaultStatus(accountId));
    }


    /**
     * 获取默认账套
     * @return
     */
    @GetMapping("/getDefaultAccountSet")
    public AjaxResult getDefaultAccountSet()
    {
        return AjaxResult.success(accountSetService.getDefaultAccountSet());
    }


    /**
     * 获取下来框选择账套
     * @return
     */
    @GetMapping("/getAccountSetListAll")
    public AjaxResult getAccountSetListAll()
    {
        List<AccountSet> list = accountSetService.getAccountSetListAll();
        return AjaxResult.success(list);
    }



    /**
     * 查询所有行业数据
     * @return
     */
    @GetMapping("getIndustryAll")
    public AjaxResult getIndustryAll()
    {
       return AjaxResult.success(accountSetService.getIndustryList());
    }


    /**
     * 查询账套列表列表
     */
    @RequiresPermissions("base:accountSet:list")
    @GetMapping("/list")
    public TableDataInfo list(AccountSet accountSet)
    {
        startPage();
        List<AccountSet> list = accountSetService.selectAccountSetList(accountSet);
        return getDataTable(list);
    }

    /**
     * 导出账套列表列表
     */
    @RequiresPermissions("base:accountSet:export")
    @Log(title = "账套列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccountSet accountSet)
    {
        List<AccountSet> list = accountSetService.selectAccountSetList(accountSet);
        ExcelUtil<AccountSet> util = new ExcelUtil<AccountSet>(AccountSet.class);
        util.exportExcel(response, list, "账套列表数据");
    }


    /**
     * 查询行业下拉框数据
     * @return
     */
    @GetMapping("/getIndustryList")
    public AjaxResult success()
    {
        List<Industry> listAll = accountSetService.getIndustryList();
        return AjaxResult.success(listAll);
    }


    /**
     * 获取账套列表详细信息
     */
    @RequiresPermissions("base:accountSet:query")
    @GetMapping(value = "/{accountId}")
    public AjaxResult getInfo(@PathVariable("accountId") Long accountId)
    {
        return success(accountSetService.selectAccountSetByAccountId(accountId));
    }

    /**
     * 新增账套列表
     */
    @RequiresPermissions("base:accountSet:add")
    @Log(title = "账套列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated AccountSet accountSet)
    {
        return toAjax(accountSetService.insertAccountSet(accountSet));
    }

    /**
     * 修改账套列表
     */
    @RequiresPermissions("base:accountSet:edit")
    @Log(title = "账套列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated AccountSet accountSet)
    {
        //判断账套id，为空不修改
        Long accountId = accountSet.getAccountId();
        if (accountId == null) {
            throw new RuntimeException("账套id为空，不能添加");
        }
        return toAjax(accountSetService.updateAccountSet(accountSet));
    }

    /**
     * 删除账套列表
     */
    @RequiresPermissions("base:accountSet:remove")
    @Log(title = "账套列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{accountIds}")
    public AjaxResult remove(@PathVariable Long[] accountIds)
    {
        if (accountIds.length == 0) {
            throw new RuntimeException("账套id为空，不能删除");
        }
        return toAjax(accountSetService.deleteAccountSetByAccountIds(accountIds));
    }
}
